﻿namespace SimdTest2
{
    public static class EnumEx
    {
        /// <summary>
        /// 获取枚举所有值
        /// </summary>
        /// <returns></returns>
        public static List<InfoIdValue> GetListAllEnumType<T>(this Type type) where T : struct
        {
            if (!type.IsEnum)
                return new List<InfoIdValue>();

            var enumValues = Enum.GetValues(type);
            var list = new List<InfoIdValue>();
            foreach (Enum value in enumValues)
            {
                list.Add(new InfoIdValue
                {
                    Id = value.GetHashCode().ToString(),
                    Value = value.GetEnumDesc(),
                    Value2 = value.ToString(),
                });
            }
            return list;
        }
        /// <summary>
        /// 根据枚举元素,获取该枚举元素的描述信息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="tField"></param>
        /// <returns></returns>
        public static string GetEnumDesc<T>(this T tField)
        {
            var description = string.Empty; //结果
            var inputType = tField.GetType(); //输入的类型
            var descType = typeof(RemarkAttribute); //目标查找的描述类型

            var fieldStr = tField.ToString();                //输入的字段字符串
            var field = inputType.GetField(fieldStr);        //目标字段

            var isDefined = field?.IsDefined(descType, false);//判断描述是否在字段的特性
            if (isDefined ?? false)
            {
                var enumAttributes = field.GetCustomAttributes(descType, false) as RemarkAttribute[];  //得到特性信息
                description = enumAttributes?.FirstOrDefault()?.Remark ?? string.Empty;
                //  description = string.Join(',', enumAttributes?.Select(t => t.Remark));
            }
            return description;
        }
    }
}
